﻿// -----------------------------------------------------------------------
// <copyright file="Gray.cs" company="FCIS">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace LastVersionOfIpPackage.Operations
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Gray
    {
        /// <summary>
        /// static value from this class
        /// </summary>
        private static Gray gray = new Gray();

        /// <summary>
        /// Gets or sets the gray1.
        /// </summary>
        /// <value>
        /// The gray1.
        /// </value>
        public static Gray Gray1
        {
            get { return Gray.gray; }
            set { Gray.gray = value; }
        }

        /// <summary>
        /// make image gray.
        /// </summary>
        /// <param name="image">The image need make in it gray.</param>
        /// <returns>
        /// the new image
        /// </returns>
        public Bitmap GrayImg(Bitmap image)
        {
            Bitmap img;
            try
            {
                Color[,] newBuffer = new Color[image.Width, image.Height];
                img = new Bitmap(image.Width, image.Height);
                Color[,] bufferOfImage = BufferOfImage.Buffer.GetBuffer(image);
                int newRed, newGreen, newBlue;
                for (int i = 0; i < image.Height; i++)
                {
                    for (int j = 0; j < image.Width; j++)
                    {
                        newRed = (bufferOfImage[j, i].R + bufferOfImage[j, i].G + bufferOfImage[j, i].B) / 3;
                        newGreen = (bufferOfImage[j, i].R + bufferOfImage[j, i].G + bufferOfImage[j, i].B) / 3;
                        newBlue = (bufferOfImage[j, i].R + bufferOfImage[j, i].G + bufferOfImage[j, i].B) / 3;
                        newBuffer[j, i] = Color.FromArgb(newRed, newGreen, newBlue);
                        img.SetPixel(j, i, newBuffer[j, i]);
                    }
                }

                return img;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw ex;
            }
        }
    }
}
